In Java, methods are used to define reusable blocks of code. Methods can be classified into different types based on their definition, usage, and functionality.
1. Predefined (Built-in) Methods
These methods are already provided by the standard libraries of Java (JDK). They belong to various classes such as
Math, String, Arrays, etc.
Example
public class PredefinedMethods {
public static void main(String[] args) {
int max = Math.max(10, 20); // Returns the maximum number
System.out.println("Maximum: " + max);
String str = "Hello";
System.out.println("Length: " + str.length()); // Returns string length
}
}
Some Common Built-in Methods
| Method | Description |
Math.max(a, b) |
Returns the maximum of two numbers a, and b |
Math.sqrt(n) |
Returns the square root of n |
String.length() |
Returns the length of a string |
Arrays.sort(array) |
Sorts an array |
2. User-Defined Methods
These are methods created by programmers to perform specific tasks. They improve the reusability and modularity of the code.
Example
public class UserDefinedMethod {
static void greet() { // Method definition
System.out.println("Hello, Welcome to Java!");
}
public static void main(String[] args) {
greet(); // Method call
}
}
3. Static Methods
A method declared with the static keyword belongs to a class rather than an
instance. It can also be called without creating an object.
Example
class StaticExample {
static void display() {
System.out.println("This is a static method.");
}
public static void main(String[] args) {
display(); // No need to create an object
}
}
Key Points
- Can be called using
ClassName.methodName(). - Non-static (instance) variables or methods cannot be used directly.
- Usually used for utility functions.
4. Instance Methods
These methods work on the instance variables of a class. They require an object to be invoked.
Example
class InstanceExample {
void show() { // Instance method
System.out.println("This is an instance method.");
}
public static void main(String[] args) {
InstanceExample obj = new InstanceExample();
obj.show(); // Calling instance method using object
}
}
Key Points
- Requires an object of the class to be called.
- Can access instance variables and other instance methods.
5. Parameterized Methods
These methods takes parameters (arguments) to perform operations based on the input values.
Example
public class ParameterizedMethod {
static void sum(int a, int b) { // Method with parameters
System.out.println("Sum: " + (a + b));
}
public static void main(String[] args) {
sum(5, 10); // Passing arguments
}
}
Key Points
- Parameters allow methods to work dynamically.
- Multiple parameters can be used
6. Method Overloading
This allows multiple methods in the same class to have the same name but different parameters (different numbers or types of arguments).
Example
class OverloadingExample {
static void show(int a) {
System.out.println("Integer: " + a);
}
static void show(String text) {
System.out.println("String: " + text);
}
public static void main(String[] args) {
show(100);
show("Hello");
}
}
Key Points
- Same method name, different parameter lists.
- The compiler decides which method to call based on the arguments.
7. Method Overriding
If a subclass defines a method with the same name, return type, and parameters as a method of the parent class, then it overrides the parent method.
Example-
public class OverrideExample {
public static void main(String[] args) {
Child obj = new Child();
obj.display(); // Calls the overridden method in Child class
}
}
class Parent {
void display() {
System.out.println("Parent class method.");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child class method.");
}
}
Key Points
- Used in inheritance to modify the behaviour of a method in a subclass.
- The
@Overrideannotation ensures correct overriding.
8. Recursive Methods
A method that calls itself is called a recursive method. It is usually used for problems like factorial calculation, Fibonacci series, etc.
Example-
class RecursionExample {
static int factorial(int n) {
if (n == 1) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
public static void main(String[] args) {
System.out.println("Factorial of 5: " + factorial(5)); // Output: 120
}
}
Main Points
- Every recursive function must have a base case to prevent infinite recursion.
- Used to solve problems that can be broken down into smaller sub-problems.
9. Abstract Methods
An abstract method is declared in an abstract class and must be implemented by subclasses. It has no body in an abstract class.
Example-
public class AbstractExample {
public static void main(String[] args) {
Dog d = new Dog();
d.makeSound(); // Output: Dog barks.
}
}
abstract class Animal {
abstract void makeSound(); // Abstract method (no body)
}
class Dog extends Animal {
void makeSound() { // Implementing the abstract method
System.out.println("Dog barks.");
}
}
Key Points
- Abstract methods must be implemented by subclasses.
- Used to enforce a common structure across subclasses.
10. Final Methods
A method declared as final cannot be overridden by subclasses.
class Parent {
final void show() {
System.out.println("Final method in Parent class.");
}
}
class Child extends Parent {
void show(){
System.out.println("Child method in Parent class.");
}// Cannot override show() method because it is final
public static void main(String[] arg)
{
Child child = new Child();
child.show();
}
}
Key Points
- Used when the behaviour of a method should not be changed in subclasses.
- Improves security and prevents unintended modifications.
Summary
Methods in Java improve code reusability and organization. By understanding the different types of methods, you can write efficient and maintainable code in Java.
Also, read: Pass by Value vs. Pass by Reference in Java
Leave Comment